<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/8/1
 * Time: 16:51
 * 视频标签
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Label extends Base {
    public function index(){
        if(request()->isAjax()){
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;

            $count = Db::table('up_video_type')->count();
            $lists = Db::table('up_video_type')
                ->order('video_type_time desc')
                ->page($Nowpage, $limits)
                ->select();//计算总页面
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        return $this->fetch();
    }

    /**
     * 添加兴趣标签
     * @return mixed|\think\response\Json
     */
    public function interestAdd(){
        if(request()->isPost()){
            $interest_word = trim(input('interest_word'), ' ');
            $isHave = Db::table('up_video_type')->where('video_type_name', $interest_word)->find();
            if($isHave){
                return json(['code' => 100, 'msg' => '该标签已存在']);
            }
            $data['video_type_name'] = $interest_word;
            $data['video_type_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_video_type')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('label/interestadd');
    }

    /**
     * 编辑兴趣标签
     * @return mixed|\think\response\Json
     */
    public function interestEdit(){
        if(Request::instance()->isPost()){
            $interest_word = input('interest_word');
            $isHave = Db::table('up_video_type')->where('video_type_name', $interest_word)->find();
            if($isHave){
                return json(['code' => 100, 'msg' => '该标签已存在']);
            }
            $id = input('interest_id');
            $res = Db::table('up_video_type')->where('video_type_id', $id)
                ->update([
                    'video_type_name' => $interest_word,
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $interest_id = input('interest_id');
        $interest_word = Db::table('up_video_type')->where('video_type_id', $interest_id)->value('video_type_name');
        $this->assign([
            'interest_id' => $interest_id,
            'interest_word' => $interest_word
        ]);
        return $this->fetch('label/interestedit');
    }

    /**
     * 删除兴趣标签
     * @return \think\response\Json
     */
    public function interestDel(){
        $video_type_id = input('id');
        $info = Db::table('up_video')->where('video_type_id',$video_type_id)->find();
        if($info['video_id']!=''){
            return json(['code' => 100, 'msg' => '已经有视频内容的标签不可删除']);
        }
        $res = Db::table('up_video_type')->delete($video_type_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}